package vivek.project.pocketmoney.expenses.scripts
{
	import flash.data.SQLConnection;

	import vivek.project.pocketmoney.admin.model.AdminConstants;
	import vivek.project.pocketmoney.admin.model.AdminDataModel;
	import vivek.project.pocketmoney.common.CommonData;
	import vivek.project.pocketmoney.common.interfaces.*;
	import vivek.project.pocketmoney.common.utils.*;
	import vivek.project.pocketmoney.expenses.model.ExpenseDataModel;

	public class ExpenseRecurssions
	{

		public function ExpenseRecurssions()
		{
			runRecurssions();
		}
		private var em:ExpenseDataModel=ExpenseDataModel.getInstance();

		public function runRecurssions():void
		{
			var reArray:Array=em.getValidExpenseRecurssions();
			var obj:Object=new Object();
			var connection:SQLConnection=CommonData.getConnection();
			connection.begin();
			for each (obj in reArray)
			{
				var startDate:Date=obj.rm_fromdate;
				var endDate:Date=obj.rm_todate;
				var amount:Number=obj.rm_amount;
				var type:int=obj.rm_type;
				var onDate:int=obj.rm_recursiondate;
				var category:int=obj.rm_category;
				var memo:String=obj.rm_memo;
				var lastExecutedDate:Date=obj.rm_last_executed_date;
				var fromAccount:int=obj.rm_fromaccount;
				var toAccount:int=obj.rm_toaccount;
				var nextExecutionDate:Date=obj.rm_nextexecutiondate;
				var rmId:int=obj.rm_id;
				var payee:int=1; //default value 
				var todayDate:Date=new Date();
				var i:int=0;
				if ((DateUtils.dateCompare(nextExecutionDate, todayDate) == 1) || DateUtils.dateCompare(nextExecutionDate, todayDate) == 0)
				{
					var nextDate:Date=nextExecutionDate; //RecurssionInterface.getNextExecutionDate(startDate,endDate,onDate);
					var toDate:Date=endDate;
					/*	||(ObjectUtil.dateCompare(nextDate,todayDate)==0)
					 ) && (ObjectUtil.dateCompare(nextDate,toDate)==1)*/
					while ((DateUtils.dateCompare(nextDate, todayDate) == 1) || (DateUtils.dateCompare(nextDate, todayDate) == 0))
					{
						var expenseRecord:Object=new Object();
						expenseRecord.account=fromAccount;
						expenseRecord.toAccount=toAccount;
						expenseRecord.category=category;
						expenseRecord.amount=amount;
						expenseRecord.memo=memo;
						expenseRecord.date=nextDate;
						expenseRecord.type=type;
						expenseRecord.transferAccount=toAccount;
						expenseRecord.payee=payee;
						expenseRecord.exRate=1;
						expenseRecord.userid=AdminDataModel.getInstance().getAccountForId(fromAccount).am_userid;
						if (type == CommonConstants.TRANSFER)
							em.addTransferRecord(expenseRecord, connection);
						else
							em.addExpense(expenseRecord, connection);
						obj.lastExecutedDate=nextDate;
						nextDate=DateUtils.dateAdd("month", 1, nextDate);
						obj.nextExecutionDate=nextDate;
						em.updateRecurrsiveExpenseNextExecutionData(obj, connection);
						i++;
					}

						//TODO:
				}
				if (i != 0)
					CommonData.applicationInfoBar.addItem(i + " Expense Recursions Added");
			}
			connection.commit();
			connection.close();
		}
	}
}